<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorate="layout">
<head>
    <title>机构入市查询</title>
</head>
<body>
<!--<h1 layout:fragment="header">Messages : View all</h1>-->
<div layout:fragment="content" class="container">
    <div class="box-header with-border">
        <h3 class="box-title">机构入市查询</h3>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form class="form-horizontal" role="form">
        <div class="box-body">
            <div class="form-group col-md-3">
                <label for="search_orgname" class="col-sm-4 control-label">机构名</label>

                <div class="col-xs-8 no-padding">
                    <input type="text" id="search_orgname" class="form-control" placeholder="机构名" name="search_orgname">
                </div>
            </div>
            <div class="form-group col-md-3">
                <label for="search_stat" class="col-sm-4 control-label">入市状态</label>

                <div class="col-xs-8 no-padding">
                    <input type="text" id="search_stat" class="form-control" placeholder="入市状态" name="search_stat">
                </div>
            </div>
            <div class="form-group col-md-4 right-button">
                <button onclick="searchBtn()" type="button" class="btn btn-danger right-button">搜索</button>
                <button onclick="addBtn()" type="button" class="btn btn-success right-button">添加</button>
                <button onclick="importBtn()" type="button" class="btn btn-danger right-button">导入</button>
                <button onclick="trunRnMkt()" type="button" class="btn btn-danger right-button">清空当前数据</button>
            </div>
        </div>
    </form>
    <div class="box-body">
        <table id="enterMarket" class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>属性值</th>
                <th>类型</th>
                <th>登记时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <div class="modal" id="enterModel">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h3 class="modal-title">新增入市信息</h3>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="enterFrom">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="add_org" class="col-sm-2 control-label">机构名</label>

                                <div class="col-sm-10">
                                    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
                                    <input type="text" class="form-control" id="add_org" data-rule="required(机构名必须填写)" name="org_name" placeholder="机构名">
                                    <input type="hidden" id="id" name="id">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="add_stat" class="col-sm-2 control-label">入市状态</label>

                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="add_stat" name="enter_stat" placeholder="入市状态">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary" onclick="enter_sub()">
                        确认
                    </button>
                    <button class="btn btn-warning" data-dismiss="modal">退出</button>
                </div>
            </div>
        </div><!-- /.modal -->
    </div>
    <div id="mkt-modal-form" class="modal" tabindex="-1" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h4 class="blue bigger">入市信息导入</h4>
                </div>
                <form action="../../admin/EnMktStat/import" id="mktFileForm" method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <fieldset>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right">导入入市信息：</label>
                                <div class="">
                                    <input type="file" id="importMktFile" name="mktFile"/>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                    <div class="modal-footer">
                        <button type="reset" class="btn btn-sm" data-dismiss="modal">
                            <i class="ace-icon fa fa-times"></i>
                            返回
                        </button>
                        <button type="button" onclick="submitMktFile()" class="btn btn-sm btn-primary">
                            <i class="ace-icon fa fa-check"></i>
                            提交
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        var table;
        $(function(){
            table=$("#enterMarket").DataTable({
                "ajax": {
                    url:"../../admin/EnMktStat/Get",
                    type:"POST",
                    dataType: "json",
                    data:function(d){
                        d.org_name=$("#search_orgname").val()
                        d.enter_stat=$("#search_stat").val()
                    },error:function(XMLHttpRequest,textStatus,errorThrown){
                        console.log("载入数据出错");
                    }
                },
                "scrollX": 1200,
                "bProcessing": true,
                "bServerSide": true,
                "bPaginate": true,
                "bAutoWidth": false,
                "bInfo": true,//页脚信息
                "bFilter": false,//禁用搜索
                "bSort":false,//禁用排序
                "aLengthMenu": [20,30,40],
                "sPaginationType":'full_numbers',
                "columns": [
                    {"data":"id"},
                    { "data": "org_name" },
                    { "data": "enter_stat" },
                    { "data": "enter_time" },
                    { "data": "id" }
                ],
                "aoColumnDefs": [
                    {
                        "aTargets": [ 4 ],
                        "mRender": function ( data, type, full ) {
                            return '<a onclick="editData(\''+full.id+'\',\''+full.org_name+'\',\''+full.enter_stat+'\')">修改</a>'
                                    +'<a onclick="delData(\''+full.id+'\')">  删除</a>';
                        }
                    }
                ],
                "fnRowCallback": function (nRow, aData, iDisplayIndex) {
                    $("td:first", nRow).html(iDisplayIndex + 1);
                    return nRow;
                },
                "oLanguage": {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "每页 _MENU_ 条",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
                    "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
                    "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
                    "sInfoPostFix": "",
                    "sSearch": "搜索：",
                    "sUrl": "",
                    "sEmptyTable": "没有数据呀!",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    }
                },
            });
            importConfig($);
        })
        //偏好使用HTML
        new SMValidator('#enterFrom');

        function searchBtn(){
            table.draw();
        }

        function addBtn(){
            $(".modal-title").text("新增入市信息")
            $("#id").val("")
            $("#add_org").val("")
            $("#add_stat").val("")
            $("#enterModel").modal("show")
        }

        function enter_sub(){
            if(SMValidator.validate('#enterFrom')){
                $.ajax({
                    url: "../../admin/EnMktStat",
                    type: "POST",
                    dataType: "json",
                    data: $("#enterFrom").serialize(),
                    beforeSend: function () {
                    },
                    success: function (data) {
                        if(data){
                            var msg = ""
                            if($("#id").val()){
                                msg = "修改成功"
                            }else {
                                msg = "新增成功"
                            }
                            Lobibox.notify('success', {
                                msg: msg,
                                sound: false
                            });
                            $("#enterModel").modal("hide")
                            table.draw();
                        }else {
                            console.log(data)
                        }
                    }, error: function () {
                    }
                });
            }
        }

        function editData(id,org_name,enter_stat){
            $(".modal-title").text("修改入市信息")
            $("#id").val(id)
            $("#add_org").val(org_name)
            $("#add_stat").val(enter_stat)
            $("#enterModel").modal("show")
        }

        function delData(id){
            Lobibox.confirm({
                msg: "确认要删除这条信息?",
                title:"删除入市信息",
                iconClass: false,
                callback: function ($this, type, ev) {
                    if(type==='yes'){
                        $.ajax({
                            url: "../../admin/EnMktStat/"+id+"?_csrf="+$("input[name=_csrf]").val(),
                            type: "DELETE",
                            dataType: "json",
                            success: function (data) {
                                if(data){
                                    Lobibox.notify('success', {
                                        msg: '删除成功',
                                        sound: false
                                    });
                                }else {
                                    Lobibox.notify('error', {
                                        msg: '删除失败',
                                        sound: false
                                    });
                                }
                                table.draw();
                            }, error: function () {
                            }
                        });
                    }
                }
            });
        }

        function trunRnMkt(){
            Lobibox.confirm({
                msg: "确认要清空数据吗?",
                title:"清空入市信息",
                iconClass: false,
                callback: function ($this, type, ev) {
                    if(type==='yes'){
                        $.ajax({
                            url:"../../admin/EnMktStat/trunEnMkt",
                            type:"GET",
                            error:function(XMLHttpRequest,textStatus,errorThrown){
                                console.log("载入数据出错");
                            },
                            dataType: "json",
                            success: function (data) {
                                if(data){
                                    table.draw();
                                    Lobibox.notify('success', {
                                        msg: '清空成功',
                                        sound: false
                                    });
                                }else {
                                    Lobibox.notify('error', {
                                        msg: '清空失败',
                                        sound: false
                                    });
                                }
                                table.draw();
                            }
                        });
                    }
                }
            });
        }

        function importBtn() {
            $("#mkt-modal-form").modal("show");
        }

        function importConfig($) {
            $("#importMktFile").ace_file_input({
                no_file:'选择文件 ...',
                btn_choose:'选择',
                btn_change:'选择',
                droppable:false,
                thumbnail:false, //| true | large
                whitelist:'xlsx|xls'
            }).on('change', function(){
                var names=$(this).data('ace_input_files')[0].name.split(".")
                if(names[names.length-1]!=="xlsx"&&names[names.length-1]!=="xls"){
                    alert("只接受xlsx或xls文件")
                    return
                }
            });
        }

        function submitMktFile() {
            $("#mktFileForm").ajaxSubmit({
                type: 'POST',
                dataType: "json",
                url:"../../admin/EnMktStat/import",
                success: function(data){
                    console.log(data);
                    if(data){
                        Lobibox.notify('success', {
                            msg: "导入成功",
                            sound: false
                        });
                        table.draw();
                    }else {
                        Lobibox.notify('error', {
                            msg: "导入失败",
                            sound: false
                        });
                    }
                    $("#mkt-modal-form").modal("hide")
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    alert( "尝试刷新网页或联系技术人员");
                }
            })
        }
    </script>
</div>
</body>
</html>
